#coding=utf-8
from bottle import hook, response, request
from lib import logs
import bottle
import time
from lib.mysql import BaseDb

@hook('before_request')
def before_request():
	if 'g' not in request:
		request.g = {}
	request.g['start_time'] = time.time()
	# request.db = BaseDb()

@hook('after_request')
def after_request():
	# request.db.close()
	request.g['end_time'] = time.time()
	log = "%s - [%s] %s - %s  %s %d  %.3f ms"
	log = log % (request.remote_addr 
		,time.strftime('%Y-%m-%d %X', time.localtime()) , request.method 
		, request.url , request.environ['SERVER_PROTOCOL'] , response.status_code 
		, (time.time()-request.g['start_time'])*1000)
	logs.settime(log)
	logs.save()

@hook('app_reset')
def app_reset():
	logs.setlog('server_restart...')